package com.skyworth.web.admin.menu.domain;


import com.skyworth.web.admin.category.domain.CategoryModel;
import com.skyworth.web.admin.category.repository.CategoryRepository;
import com.skyworth.web.common.domain.AttachmentModel;
import com.skyworth.web.common.repository.AttachmentRepository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Component;

import javax.persistence.*;

/**
 * Created by skyworth on 2016/8/8.
 */
@Entity
@Component
@Table(name="ff_menu")
public class MenuModel {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Setter
	@Getter
	public long id;

    @Column(name = "store_id")
    @Setter
    @Getter
    public long storeId;

	@Column(name = "category_id")
	public long categoryId;

	@Column(name="name")
	@Setter
	@Getter
	public String name;

	@Column(name="first_letter")
	public String firstLetter;


	@Column(name="unit_price")
	@Setter
	@Getter
	public float unitPrice;

	@Column(name="unit_name")
	@Setter
	@Getter
	public String unitName;

//	@Range(min=0.1,max=9.9,message="折扣范围0.1-9.9")
//	@DecimalMin(value="0.1")
//	@DecimalMax(value="9.9")
	@Column(name="discount")
	@Setter
	@Getter
	public float discount;

	@Column(name="recommend")
	@Setter
	@Getter
	public int recommend;

	@Column(name="stop_sale")
	@Setter
	@Getter
	public int stopSale;

	@Column(name="sold_out")
	@Setter
	@Getter
	public int soldOut;

	@Column(name="img_attachment_id")
	@Setter
	@Getter
	public long imgId;

	@Column(name="remove_flag")
	@Setter
	@Getter
	public int removeFlag;


    @Transient
	private static AttachmentRepository attachmentRepository;

	@Transient
	private static CategoryRepository categoryRepository;

	@Autowired
	public void setCategoryRepository(CategoryRepository categoryRepository) {
		MenuModel.categoryRepository = categoryRepository;
	}

	@Autowired
	public void setAttachmentRepository(AttachmentRepository attachmentRepository) {
		MenuModel.attachmentRepository = attachmentRepository;
	}

	public String getCategoryName() {
		CategoryModel categoryModel = MenuModel.categoryRepository.findOne(this.categoryId);
		if(null == categoryModel){
			return "奇葩";
		}
		return categoryModel.name;
	}

	public String getRecommendFlag(){
		return (recommend==1)? "是":"否";
	}

	public String getStopSaleFlag(){
		return (stopSale==1)? "是":"否";
	}

	public boolean hasMenuPic(){
		AttachmentModel item = MenuModel.attachmentRepository.findOne(this.imgId);
		if (null == item) {
			return false;
		}
		return true;
	}
	public String getMenuPicUrl() {
		AttachmentModel item = MenuModel.attachmentRepository.findOne(this.imgId);
		if (null == item) {
			return null;
		}
		return item.url;
	}

	public long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	public String getFirstLetter() {
		return firstLetter;
	}

	public void setFirstLetter(String firstLetter) {
		this.firstLetter = firstLetter;
	}
}
